﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using KTool2.Utility;
using KTool2.Common;
using System.Windows.Threading;
using KTool2.CustomEvent;
using KTool2.Impl;
using KTool2.Exceptions;

namespace KTool2
{
    enum SetTextMode { Next, Previous }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //private AppSetting settings;
        private DispatcherTimer s_textTimer;
        private Core.IKanjiWordList t_kWordList;
        private System.Windows.Media.Animation.DoubleAnimation t_textAnimationBegin;
        private System.Windows.Media.Animation.DoubleAnimation t_textAnimationEnd;
        private KanjiWordHistoryList t_historyList;
        private SetTextMode nextMode = SetTextMode.Next;
        private SetTextMode previousMode = SetTextMode.Previous;
        //private InfoWindow infoWindow;
        //private SearchWindow searchWindow;
        //private TranslateConditionWindow TranslateConditionWindow.Instance;
        //private TextChangeTimeWindow textChangeTimeWindow;
        //private AboutBox aboutBox;
        private double movingOffset = 0;

        public MainWindow()
        {
            try
            {
                InitializeComponent();
                this.Top = System.Windows.SystemParameters.WorkArea.Height - this.Height - 5;
                this.Left = System.Windows.SystemParameters.WorkArea.Width - this.Width - 5;

                InitializeSetting();

                //this.infoWindow = new InfoWindow();
                InfoWindow.Instance.ShowHideChanged += new InfoWindow.WindowShowHideHandler(infoWindow_ShowHideChanged);
                InfoWindow.Instance.ParentActivated += new InfoWindow.ActivationHandler(infoWindow_Activated);
                this.SetInfoWindowLocation();

                InitializeImage();
                InitializeTextAnimation();
                InitializeText();

                this.SetTextBegin(nextMode);
                this.s_textTimer.Start();
            }
            catch (DBException ex)
            {
                this.ShowErrorMessage("Error while getting kanji information:\n" + ex.Description + "\nProgram will now exit!");
                this.Close();
            }
            catch (Exception e)
            {
                this.ShowErrorMessage("Error in initializing!\n" + e.Message + "\nProgram will now exit!");
                this.Close();
            }
        }

        void infoWindow_Activated()
        {
            this.Activate();
        }

        private void SetInfoWindowLocation()
        {
            InfoWindow.Instance.Top = this.Top + this.Height;
            InfoWindow.Instance.Left = this.Left;
        }

        void infoWindow_ShowHideChanged(object sender, WindowShowHideEventArgs e)
        {
            if (e.IsShowing)
            {
                if (System.Windows.SystemParameters.WorkArea.Height
                    - this.Height - this.Top < InfoWindow.Instance.Height)
                {
                    this.movingOffset = System.Windows.SystemParameters.WorkArea.Height - this.Height - InfoWindow.Instance.Height - 5 - this.Top;
                    this.Top = System.Windows.SystemParameters.WorkArea.Height - this.Height - InfoWindow.Instance.Height - 5;
                }
            }
            else
            {
                this.Top -= this.movingOffset;
            }
        }

        private void InitializeImage()
        {
            this.window.Background = ImageManager.MetalBackGround;
            this.rate1.Source = ImageManager.Dot;
            this.rate2.Source = ImageManager.Dot;
            this.rate3.Source = ImageManager.Dot;
            this.rate4.Source = ImageManager.Dot;
            this.rate5.Source = ImageManager.Dot;
            this.btnClose.Source = ImageManager.CloseButton;
            this.btnQuestion.Source = ImageManager.QuestionButton;
            this.grdScreen.Background = ImageManager.Screen;
            this.btnNext.Source = ImageManager.NextButton;
            this.btnPrevious.Source = ImageManager.PreviousButton;
        }

        private void InitializeSetting()
        {
            try
            {
                AppSetting.LoadLastSetting();
            }
            catch (Exception e)
            {
                if (e is ApplicationSettingException)
                {
                    this.ShowErrorMessage(((ApplicationSettingException)e).Description + "\nApplication settings are now set to default.");
                }
                else
                {
                    this.ShowErrorMessage(e.Message + "\nApplication settings are now set to default.");
                }
                AppSetting.LoadDefaultSetting();
            }

            AppSetting.Instance.SettingChanged += new AppSetting.SettingChangeEventHandler(App_SettingChanged);

            this.s_textTimer = new DispatcherTimer();
            this.s_textTimer.Tick += new EventHandler(S_TextTimer_Tick);
            this.SetS_STextTimer();
        }

        private void SetS_STextTimer()
        {
            this.s_textTimer.Interval = AppSetting.Instance.MeanTime;
        }

        private void S_TextTimer_Tick(object sender, EventArgs e)
        {
            this.SetTextBegin(nextMode);
        }

        private void InitializeText()
        {
            this.t_historyList = new KanjiWordHistoryList();
            this.btnPrevious.IsEnabled = false;
            this.btnPrevious.Opacity = 0.5;
            this.SetT_JPNCategoriesMenuItem();
            this.SetList();
        }

        private void SetList()
        {
            try
            {
                if (AppSetting.Instance.Category.Equals(Constant.ALL_CATEGORIES))
                {
                    this.t_kWordList = KanjiWordListProcessor.GetAllKanjiWord();
                }
                else
                {
                    this.t_kWordList = KanjiWordListProcessor.GetKanjiWordList(AppSetting.Instance.Category);
                }

                this.t_kWordList.Shuffle();
            }
            catch (DBException ex)
            {
                if (ex.ErrorCode == 2) // Sheet not found
                {
                    this.ShowErrorMessage("Error while reading previous setting JPN categories:\n" + ex.Description + "\nProgram will reset JPN categories to " + Constant.ALL_CATEGORIES);
                    AppSetting.Instance.Category = Constant.ALL_CATEGORIES;
                    //this.SetList();
                }
                else
                {
                    this.ShowErrorMessage("Error while getting kanji information:\n" + ex.Description + "\nProgram will now exit!");
                    this.Close();
                }
            }
        }

        private void SetT_JPNCategoriesMenuItem()
        {
            string[] tableArray = Database.DBManagement.TableArray;
            this.mnuiJPNCategories.Items.Clear();
            MenuItem category = null;

            foreach (string table in tableArray)
            {
                category = new MenuItem();
                category.Name = "mnuiJPNCategory" + table.Replace('#', '_').Replace(' ', '_').Substring(0, table.Length - 1);
                category.Header = table.Replace('#', '.');
                this.mnuiJPNCategories.Items.Add(category);

                if (AppSetting.Instance.Category.Equals(category.Header.ToString()))
                {
                    category.IsChecked = true;
                }
            }

            this.mnuiJPNCategories.Items.Add(new Separator());

            category = new MenuItem();
            category.Name = "mnuiJPNCategory" + Constant.ALL_CATEGORIES.Replace('#', '_').Replace(' ', '_');
            category.Header = Constant.ALL_CATEGORIES;
            this.mnuiJPNCategories.Items.Add(category);
            if (AppSetting.Instance.Category.Equals(Constant.ALL_CATEGORIES))
            {
                category.IsChecked = true;
            }
        }

        private void InitializeTextAnimation()
        {
            this.t_textAnimationBegin = new System.Windows.Media.Animation.DoubleAnimation();
            this.t_textAnimationBegin.To = 0;
            this.t_textAnimationBegin.Duration = TimeSpan.FromSeconds(Constant.TEXT_ANIMATION_TIME);

            this.t_textAnimationEnd = new System.Windows.Media.Animation.DoubleAnimation();
            this.t_textAnimationEnd.To = 1;
            this.t_textAnimationEnd.Duration = TimeSpan.FromSeconds(Constant.TEXT_ANIMATION_TIME);
        }

        private void SetTextBegin(SetTextMode mode)
        {
            this.t_textAnimationBegin.Completed -= this.SetTextNextEnd;
            this.t_textAnimationBegin.Completed -= this.SetTextPreviousEnd;

            if (mode == SetTextMode.Next)
            {
                this.t_textAnimationBegin.Completed += this.SetTextNextEnd;
            }
            else
            {
                if (!this.t_historyList.HasPrevious)
                {
                    return;
                }

                this.t_textAnimationBegin.Completed += this.SetTextPreviousEnd;
            }

            this.s_textTimer.Stop();
            this.txtblkKanji.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationBegin);
            InfoWindow.Instance.SetTextBegin();
        }

        private void SetTextNextEnd(object sender, EventArgs e)
        {
            try
            {
                if (this.t_historyList.HasNext)
                {
                    this.txtblkKanji.Text = this.t_historyList.Next.Kanji;
                }
                else
                {
                    this.t_historyList.Add(this.t_kWordList.RanDomKanji);
                    this.txtblkKanji.Text = this.t_historyList.Head.Kanji;
                }

                this.SetMemRate(this.t_historyList.Current.MemRate);
                InfoWindow.Instance.SetTextEnd(this.t_historyList.Current);

                if (this.t_historyList.HasPrevious)
                {
                    this.btnPrevious.IsEnabled = true;
                    this.btnPrevious.Opacity = 1;
                }

                this.txtblkKanji.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
                this.s_textTimer.Start();
            }
            catch (DBException ex)
            {
                this.ShowErrorMessage("Error while getting kanji information:\n" + ex.Description + "\nProgram will now exit!");
                this.Close();
            }
        }

        private void SetTextPreviousEnd(object sender, EventArgs e)
        {
            try
            {
                this.txtblkKanji.Text = this.t_historyList.Previous.Kanji;
                this.SetMemRate(this.t_historyList.Current.MemRate);
                InfoWindow.Instance.SetTextEnd(this.t_historyList.Current);

                if (!this.t_historyList.HasPrevious)
                {
                    this.btnPrevious.IsEnabled = false;
                    this.btnPrevious.Opacity = 0.5;
                }

                this.txtblkKanji.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
                this.s_textTimer.Start();
            }
            catch (DBException ex)
            {
                this.ShowErrorMessage("Error while getting kanji information:\n" + ex.Description + "\nProgram will now exit!");
                this.Close();
            }
        }

        private void SetMemRate(int memRate)
        {
            this.ResetMemRate();

            if (memRate >= 1)
            {
                this.rate1.Source = ImageManager.Star;
            }

            if (memRate >= 2)
            {
                this.rate2.Source = ImageManager.Star;
            }

            if (memRate >= 3)
            {
                this.rate3.Source = ImageManager.Star;
            }

            if (memRate >= 4)
            {
                this.rate4.Source = ImageManager.Star;
            }

            if (memRate == 5)
            {
                this.rate5.Source = ImageManager.Star;
            }
        }

        private void ResetMemRate()
        {
            this.rate1.Source = ImageManager.Dot;
            this.rate2.Source = ImageManager.Dot;
            this.rate3.Source = ImageManager.Dot;
            this.rate4.Source = ImageManager.Dot;
            this.rate5.Source = ImageManager.Dot;
        }

        private void ShowErrorMessage(string message)
        {
            MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void ShowAlternateWindow(Window instance)
        {
            //this.searchWindow = SearchWindow.Instance;
            //SearchWindow.Instance.Closing -= AlternateWindow_Closing;
            //SearchWindow.Instance.Closing += AlternateWindow_Closing;
            //SearchWindow.Instance.Show();
            //SearchWindow.Instance.Activate();
            //SearchWindow.Instance.WindowState = System.Windows.WindowState.Normal;
            instance.Closing -= AlternateWindow_Closing;
            instance.Closing += AlternateWindow_Closing;
            instance.Show();
            instance.Activate();
            instance.WindowState = System.Windows.WindowState.Normal;

            this.Hide();

            if (!InfoWindow.IsNull)
            {
                InfoWindow.Instance.HideWindow();
            }

            if (!TextChangeTimeWindow.IsNull)
            {
                TextChangeTimeWindow.Instance.Hide();
            }

            if (!TranslateConditionWindow.IsNull)
            {
                TranslateConditionWindow.Instance.Hide();
            }

            this.s_textTimer.Stop();
        }

        private void ZoomIn()
        {
            this.grdMain.Width = this.window.Width;
            this.grdMain.Height = this.window.Height;

            this.window.Height = this.window.Height * 1.5;
            this.window.Width = this.window.Width * 1.5;

            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 1.5;
            st.ScaleY = 1.5;
            st.CenterX = this.grdMain.Width / 2;
            st.CenterY = this.grdMain.Height / 2;

            this.grdMain.RenderTransform = st;
        }

        private void ZoomInTask()
        {
            this.ZoomIn();
            if (!InfoWindow.IsNull)
            {
                InfoWindow.Instance.ZoomIn();
            }
            this.mnuiZoomIn.IsEnabled = false;
            this.mnuiZoomOut.IsEnabled = true;
            this.movingOffset = 0;
            this.SetInfoWindowLocation();
        }

        private void ZoomOut()
        {
            this.window.Height = this.window.Height * 2.0 / 3;
            this.window.Width = this.window.Width * 2.0 / 3;

            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 1;
            st.ScaleY = 1;
            st.CenterX = this.grdMain.Width / 2.0;
            st.CenterY = this.grdMain.Height / 2.0;

            this.grdMain.RenderTransform = st;
        }

        private void ZoomOutTask()
        {
            this.ZoomOut();
            if (!InfoWindow.IsNull)
            {
                InfoWindow.Instance.ZoomOut();
            }
            this.mnuiZoomIn.IsEnabled = true;
            this.mnuiZoomOut.IsEnabled = false;
            this.movingOffset = 0;
            this.SetInfoWindowLocation();
        }

        #region Event Handling

        private void ActivateComponent()
        {
            this.window.Background = ImageManager.MetalBackGround;
            this.btnClose.Source = ImageManager.CloseButton;
            this.btnQuestion.Source = ImageManager.QuestionButton;
            this.grdScreen.Background = ImageManager.Screen;
        }

        private void DeactivateComponent()
        {
            this.window.Background = ImageManager.MetalBackGroundDeactivated;
            this.btnClose.Source = ImageManager.CloseButtonDeactivated;
            this.btnQuestion.Source = ImageManager.QuestionButtonDeactivated;
            this.grdScreen.Background = ImageManager.ScreenDeactivated;
        }

        private void Component_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is Image)
            {
                if ("btnClose".Equals(((Image)sender).Name))
                {
                    this.window.MouseDown -= this.window_MouseDown;
                    this.btnClose.Source = ImageManager.CloseButtonEnter;
                }
                else if ("btnQuestion".Equals(((Image)sender).Name))
                {
                    this.window.MouseDown -= this.window_MouseDown;
                    this.btnQuestion.Source = ImageManager.QuestionButtonEnter;
                }
            }
            else if (sender is Grid)
            {
                if ("grdScreen".Equals(((Grid)sender).Name))
                {
                    this.window.MouseDown -= this.window_MouseDown;
                }
            }
        }

        private void Component_MouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is Image)
            {
                if ("btnClose".Equals(((Image)sender).Name))
                {
                    this.window.MouseDown += this.window_MouseDown;
                    if (this.IsActive)
                    {
                        this.btnClose.Source = ImageManager.CloseButton;
                    }
                    else
                    {
                        this.btnClose.Source = ImageManager.CloseButtonDeactivated;
                    }
                }
                else if ("btnQuestion".Equals(((Image)sender).Name))
                {
                    this.window.MouseDown += this.window_MouseDown;
                    if (this.IsActive)
                    {
                        this.btnQuestion.Source = ImageManager.QuestionButton;
                    }
                    else
                    {
                        this.btnQuestion.Source = ImageManager.QuestionButtonDeactivated;
                    }
                }
            }
            else if (sender is Grid)
            {
                if ("grdScreen".Equals(((Grid)sender).Name))
                {
                    this.window.MouseDown += this.window_MouseDown;
                }
            }
        }

        private void Component_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if ("btnClose".Equals(((Image)sender).Name))
            {
                this.btnClose.Source = ImageManager.CloseButtonDown;
            }
            else if ("btnQuestion".Equals(((Image)sender).Name))
            {
                this.btnQuestion.Source = ImageManager.QuestionButtonDown;
            }
            else if ("btnNext".Equals(((Image)sender).Name))
            {
                this.btnNext.Source = ImageManager.NextButtonDown;
            }
            else if ("btnPrevious".Equals(((Image)sender).Name))
            {
                this.btnPrevious.Source = ImageManager.PreviousButtonDown;
            }
            else if (((Image)sender).Name.StartsWith("rate"))
            {
                try
                {
                    //this.t_historyList.Current.MemRate = ((Image)sender).Name[4] - '0';
                    this.t_kWordList.SetMemrate(this.t_historyList.Current, ((Image)sender).Name[4] - '0');
                }
                catch (DBException ex)
                {
                    this.ShowErrorMessage("Error while setting memrate:\n" + ex.Description + "\nProgram will now exit!");
                    this.Close();
                }
                this.SetMemRate(this.t_historyList.Current.MemRate);
            }
        }

        private void Component_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if ("btnClose".Equals(((Image)sender).Name))
            {
                this.btnClose.Source = ImageManager.CloseButtonEnter;
                this.Close();
            }
            else if ("btnQuestion".Equals(((Image)sender).Name))
            {
                this.btnQuestion.Source = ImageManager.QuestionButtonEnter;
                InfoWindow.Instance.ShowWindow();
            }
            else if ("btnNext".Equals(((Image)sender).Name))
            {
                this.btnNext.Source = ImageManager.NextButton;
                this.SetTextBegin(nextMode);
            }
            else if ("btnPrevious".Equals(((Image)sender).Name))
            {
                this.btnPrevious.Source = ImageManager.PreviousButton;
                this.SetTextBegin(previousMode);
            }
        }

        private void window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        private void window_Activation(object sender, EventArgs e)
        {
            if (this.IsActive)
            {
                this.ActivateComponent();
                InfoWindow.Instance.ActivateWindow();
            }
            else
            {
                this.DeactivateComponent();
                InfoWindow.Instance.DeactivateWindow();
            }
        }

        private void window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right)
            {
                this.SetTextBegin(nextMode);
            }
            else if (e.Key == Key.Left)
            {
                this.SetTextBegin(previousMode);
            }
            //else if (e.Key == Key.F)
            //{
            //    ShowSearchWindow();
            //}
        }

        private void menuItem_Click(object sender, RoutedEventArgs e)
        {
            if (((MenuItem)sender).Name.StartsWith("mnuiJPNCategory"))
            {
                foreach (object item in this.mnuiJPNCategories.Items)
                {
                    if (item is MenuItem && item == (MenuItem)sender && !((MenuItem)item).IsChecked)
                    {
                        AppSetting.Instance.Category = ((MenuItem)item).Header.ToString();
                        break;
                    }
                }
            }
            else
            {
                switch (((MenuItem)sender).Name)
                {
                    case "mnuiAutoStart":
                        AppSetting.Instance.AutoStart = !AppSetting.Instance.AutoStart;
                        break;
                    case "mnuiTranslateCondition":
                        //transConWindow = TranslateConditionWindow.GetInstance(ref this.settings);
                        TranslateConditionWindow.Instance.Show();
                        TranslateConditionWindow.Instance.WindowState = System.Windows.WindowState.Normal;
                        TranslateConditionWindow.Instance.Activate();
                        break;
                    case "mnuiTextChangeTime":
                        //textChangeTimeWindow = TextChangeTimeWindow.GetInstance(ref this.settings);
                        TextChangeTimeWindow.Instance.Show();
                        TextChangeTimeWindow.Instance.WindowState = System.Windows.WindowState.Normal;
                        TextChangeTimeWindow.Instance.Activate();
                        break;
                    case "mnuiAbout":
                        //AboutBox.Instance = AboutBox.Instance;
                        AboutBox.Instance.Show();
                        AboutBox.Instance.WindowState = System.Windows.WindowState.Normal;
                        AboutBox.Instance.Activate();
                        break;
                    case "mnuiSearch":
                        ShowAlternateWindow(SearchWindow.Instance);
                        break;
                    case "mnuiHiraganaTest":
                        ShowAlternateWindow(TestWindow.Instance);
                        break;
                    case "mnuiZoomIn":
                        this.ZoomInTask();
                        break;
                    case "mnuiZoomOut":
                        this.ZoomOutTask();
                        break;
                }
            }
        }

        void AlternateWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.s_textTimer.Start();
            this.Show();

            if (!InfoWindow.IsNull)
            {
                InfoWindow.Instance.SetTextEnd(this.t_historyList.Current);
                //InfoWindow.Instance.ShowWindow();
                //InfoWindow.Instance.SetTextBegin();
            }

            if (!TextChangeTimeWindow.IsNull)
            {
                TextChangeTimeWindow.Instance.Show();
            }

            if (!TranslateConditionWindow.IsNull)
            {
                TranslateConditionWindow.Instance.Show();
            }
        }

        private void window_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (e.Key == Key.F)
                {
                    this.ShowAlternateWindow(SearchWindow.Instance);
                }
                else if (e.Key == Key.OemPlus)
                {
                    if (this.mnuiZoomIn.IsEnabled)
                    {
                        this.ZoomInTask();
                    }
                }
                else if (e.Key == Key.OemMinus)
                {
                    if (this.mnuiZoomOut.IsEnabled)
                    {
                        this.ZoomOutTask();
                    }
                }
            }
            else if (e.Key.ToString()[0] == Key.D1.ToString()[0] && e.Key.ToString()[1] >= Key.D1.ToString()[1]
                && e.Key.ToString()[1] <= Key.D5.ToString()[1])
            {
                this.t_historyList.Current.MemRate = e.Key.ToString()[1] - '0';
                this.SetMemRate(e.Key.ToString()[1] - '0');
            }
        }

        private void App_SettingChanged(object sender, SettingEventArgs e)
        {
            switch (e.ChangedItem)
            {
                case AppProperties.AUTO_START:
                    if (!this.mnuiAutoStart.IsChecked)
                    {
                        StartUpUtil.SetAutoStart(Constant.ASSEMBLY_NAME, Constant.ASSEMBLY_LOCATION);
                        this.mnuiAutoStart.IsChecked = true;
                    }
                    else
                    {
                        if (StartUpUtil.IsAutoStartEnabled(Constant.ASSEMBLY_NAME, Constant.ASSEMBLY_LOCATION))
                        {
                            StartUpUtil.UnSetAutoStart(Constant.ASSEMBLY_NAME);
                        }
                        this.mnuiAutoStart.IsChecked = false;
                    }
                    break;
                case AppProperties.CATEGORY:
                    this.InitializeText();
                    this.SetTextBegin(nextMode);
                    this.s_textTimer.Start();
                    break;
                case AppProperties.TEXT_CHANGE_TIME:
                    this.s_textTimer.Stop();
                    this.s_textTimer.Interval = AppSetting.Instance.MeanTime;
                    this.s_textTimer.Start();
                    break;
                case AppProperties.RATE_SHOWING_ARRAY:
                    // Do nothing, automatically raise event
                    break;
                case AppProperties.KEEP_TRANSLATION:
                    break;
            }
        }

        private void window_LocationChanged(object sender, EventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && this.IsMouseOver)
            {
                this.movingOffset = 0;
            }

            this.SetInfoWindowLocation();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            if (Utility.StartUpUtil.IsAutoStartEnabled(Constant.ASSEMBLY_NAME, Constant.ASSEMBLY_LOCATION))
            {
                this.mnuiAutoStart.IsChecked = true;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            AppSetting.SaveSetting(AppSetting.Instance);

            if (!InfoWindow.IsNull)
            {
                InfoWindow.Instance.Close();
            }

            if (!SearchWindow.IsNull)
            {
                SearchWindow.Instance.Close();
            }

            if (!TextChangeTimeWindow.IsNull)
            {
                TextChangeTimeWindow.Instance.Close();
            }

            if (!TranslateConditionWindow.IsNull)
            {
                TranslateConditionWindow.Instance.Close();
            }

            if (!AboutBox.IsNull)
            {
                AboutBox.Instance.Close();
            }

            if (!TestWindow.IsNull)
            {
                TestWindow.Instance.Close();
            }
        }

        #endregion
    }
}
